package trong.gold.soft.com.model;

import javax.microedition.lcdui.Image;

/**
 *
 * @author trong
 */
public class ResizeImage {
// public Image newIm=null;

    public static Image resize(Image oldIm, int newW, int newH) {

        Image newIm = null;
        int oldW = oldIm.getWidth();
        int oldH = oldIm.getHeight();

        // táº¡o 1 mÃ£ng chá»©a cÃ¡c RGB
        int[] arrOld = new int[oldW * oldH];
        int[] arrNew = new int[newW * newH];

        oldIm.getRGB(arrOld, 0, oldW, 0, 0, oldW, oldH);
        //mÃ£ng dá»¯ liá»‡u cÅ©,Ä‘á»‹a chÄ© oFfset,Ä‘á»™ dÃ i 1 hÃ ng,tá»�a Ä‘á»™ gÃ³c trÃªn,tá»�a Ä‘á»™ gÃ³c dÆ°á»›i
        if (newW < newH) {
            for (int i = 0; i < newH; i++) {
                int temp = i * oldH / newH;
                for (int j = 0; j < newW; j++) {
                    arrNew[i * newW + j] = arrOld[temp * oldW + j * oldW / newW];
                }
            }
            //táº¡o 1 IMAGE báº±ng dá»¯ liá»‡u cÅ©
            newIm = Image.createRGBImage(arrNew, newW, newH, true);
        }else{
          for (int i = 0; i < newH; i++) {
                int temp = i * oldH / newH;
                for (int j = 0; j < newW; j++) {
                    arrNew[i * newW + j] = arrOld[temp/2 * oldW + j * oldW / newW];
                }
            }
            //táº¡o 1 IMAGE báº±ng dá»¯ liá»‡u cÅ©
            newIm = Image.createRGBImage(arrNew,newW, newH, true);
        }
        // Ã¡nh xáº¡ tá»«ng Ä‘iá»ƒm qua bÃªn New
        return newIm;
    }
}